package com.eclipse.training.extensionpoint;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;

import com.eclipse.training.extensions.IMyExtension;
import com.eclipse.training.extensions.LogManager;

public class MyExtension implements IMyExtension {

	public MyExtension() {
		LogManager.getLogger().logInfo("The extension class has been built!");
	}

	@Override
	public boolean doSomeWork(Object input) {
		if (input instanceof IFile) {

			IFile file = ((IFile) input);
			IPath path = file.getParent().getFullPath();
			String ext = file.getFileExtension();
			String name = file.getName().replace("." + ext, "_copy." + ext);

			try {
				file.copy(new Path(path.toString() + "\\" + name), false,
						new NullProgressMonitor());

				LogManager.getLogger().logInfo(
						"The extension has been executed on " + input);
				return true;
			} catch (CoreException e) {
				LogManager.getLogger().logError(e);
			}
		}
		return false;
	}

}
